ALTER PROC sp_aged_backlog_report
@PRODUCT_IDS VARCHAR(50),
@CUSTOMER_ID INT,
@CATEGORY VARCHAR(50),
@PERIODICAL VARCHAR(50),
@DATE_FROM  VARCHAR(50),
@DATE_T0  VARCHAR(50),
@CURRENT_QUARTER VARCHAR(4),
@CURRENT_YEAR VARCHAR(4),
@DEFAULT_PROMPT VARCHAR(20),
@FG_PRODUCT_ID VARCHAR(50),
@CUSTOMER_NAME VARCHAR(50)

AS

DECLARE @PROCESS_STEP_ID INT 
DECLARE @TEMP_PROCESS_STEPS_TABLE TABLE( ProcessStep_ID INT )
DECLARE @PROCESS_STEPS_LOCATION TABLE( ProcessStep_ID INT, Location_ID INT )
DECLARE @TEMP_PRODUCT_ID INT
DECLARE @REQUESTED_BACKLOG_QTY INT
DECLARE @SCHEDULED_BACKLOG_QTY INT
DECLARE @REVISED_BACKLOG_QTY INT
DECLARE @TEMP_FROM_DATE DATETIME
DECLARE @TEMP_TO_DATE DATETIME
DECLARE @TEMP_PS_TYPE VARCHAR(20)
DECLARE @L_FROM_DATE  VARCHAR(20)
DECLARE @L_TO_DATE  VARCHAR(20)
DECLARE @SCH_DATE VARCHAR(50)
DECLARE @REQ_DATE VARCHAR(50)
DECLARE @REV_DATE VARCHAR(50)

BEGIN
	DECLARE @PROCESS_STEPS_TABLE TABLE( ProcessStep_ID INT )
	DECLARE @RESULT_BASEPRODUCTNAME VARCHAR(50)
	DECLARE @RESULT_BASE_PRODUCT_ID VARCHAR(20)
	DECLARE @RESULT_PROCESS_STEP_ID INT
	DECLARE @RESULT_PRODUCT_ID INT
	DECLARE @RESULT_PRODUCT VARCHAR(50)
	DECLARE @RESULT_PS_TYPE VARCHAR(50)
	DECLARE @RESULT_PRODUCT_TYPE VARCHAR(50)
	DECLARE @RESULT_PRODUCT_TYPE_ID INT
	DECLARE @RESULT_REQ_SCH_DATE DATETIME
	DECLARE @RESULT_SHIPPED_QTY BIGINT
	DECLARE @RESULT_UNIT_COST FLOAT
	DECLARE @RESULT_AMOUNT FLOAT
	DECLARE @PS_SHIPPED_QTY INT


	DECLARE @REQUESTED_DATE DATETIME
	DECLARE @SCHEDULED_DATE DATETIME
	DECLARE @BOOKING_DATE DATETIME
	DECLARE @TEMP_SHIPPED_QTY BIGINT
	DECLARE @TEMP_UNIT_COST FLOAT
	DECLARE @TEMP_LINEITEM_ID INT
	

	DECLARE @SHIP_REQ_SCH_DT DATETIME 
	DECLARE @SHIP_YR_I INT
	DECLARE @SHIP_MT_I INT
	DECLARE @SHIP_QT_I INT
	DECLARE @SHIP_QT_END_I INT
	DECLARE @ACT_YEAR INT
	
	DECLARE @CURRENT_QTR_DATE DATETIME 
	DECLARE @CURRENT_QTR_END_DATE DATETIME
	DECLARE @CURRENT_QTR_MONTH INT
	DECLARE @CURRENT_QTR_QTR INT
	DECLARE @CURRENT_QTR_QTR_S INT
	DECLARE @CURRENT_QTR_QTR_E INT
	DECLARE @CURRENT_QTR_YEAR INT

	DECLARE @IS_START_BACKLOG BIT

	DECLARE @CHK_PRODUCT_FOR_BOARD_OR_KIT VARCHAR(50)
	DECLARE @TEMP_DATE VARCHAR(20)	

	INSERT INTO @PROCESS_STEPS_TABLE( ProcessStep_ID ) SELECT ProcessStep_ID FROM FN_GET_PROCESS_STEPS_IN_STRING( @PRODUCT_IDS )

	DECLARE @RESULTS_TABLE TABLE ( BaseProductName VARCHAR(50),
					BaseProductID VARCHAR(20),
					ProcessStepID INT,
					ProductID INT,
					Product VARCHAR(50),
					PS_Type VARCHAR(50),
					Product_Type VARCHAR(50),
					Product_Type_ID INT,
					Req_Sch_Date DATETIME,
					Shipped_Qty INT, 
					Unit_Cost FLOAT,
					Amount FLOAT,
					ShipmentMonth INT, 
					ShipmentYear INT, 
					QuarterStart INT, 
					QuarterEnd INT, 
					ActYear INT,
					IsStartBackLog Bit)	


	DECLARE @SEARCH_RESULTS_TABLE TABLE ( BaseProductName VARCHAR(50),
					BaseProductID VARCHAR(20),
					ProcessStepID INT,
					ProductID INT,
					Product VARCHAR(50),
					PS_Type VARCHAR(50),
					Product_Type VARCHAR(50),
					Product_Type_ID INT,
					Req_Sch_Date DATETIME,
					Shipped_Qty INT, 
					Unit_Cost FLOAT,
					Amount FLOAT,
					ShipmentMonth INT, 
					ShipmentYear INT, 
					QuarterStart INT, 
					QuarterEnd INT, 
					ActYear INT,
					IsStartBackLog Bit)	

	DECLARE @PARENT_PRODUCT_CURSOR CURSOR
	DECLARE @LINE_ITEM_CURSOR CURSOR


	SET @RESULT_BASEPRODUCTNAME  	= NULL	
	SET @RESULT_PROCESS_STEP_ID 	= NULL
	SET @RESULT_PRODUCT_ID 		= NULL
	SET @RESULT_PRODUCT  		= NULL

	SET @CURRENT_QTR_DATE 	= NULL
	SET @CURRENT_QTR_END_DATE = NULL
	SET @CURRENT_QTR_MONTH 	= NULL
	SET @CURRENT_QTR_QTR	= NULL
	SET @CURRENT_QTR_QTR_S 	= NULL
	SET @CURRENT_QTR_QTR_E 	= NULL
	SET @CURRENT_QTR_YEAR 	= NULL


	SET @CURRENT_QTR_YEAR 	= YEAR(CURRENT_TIMESTAMP)
	SET @CURRENT_QTR_QTR = DATEPART( Q, CURRENT_TIMESTAMP )
	--SET @CURRENT_QTR_MONTH 	= MONTH(CURRENT_TIMESTAMP)
	--SET @SHIP_REQ_SCH_DT = CONVERT( DATETIME, CURRENT_TIMESTAMP, 101)
	--PRINT ' @CURRENT_QTR_QTR = ' + CONVERT(VARCHAR,@CURRENT_QTR_QTR) + ' @CURRENT_QTR_YEAR = ' + CONVERT(VARCHAR,@CURRENT_QTR_YEAR)

	IF ( @CURRENT_QTR_QTR = '1' )
	BEGIN
		SET @CURRENT_QTR_MONTH = 1
		SET @CURRENT_QTR_QTR_S = 1
		SET @CURRENT_QTR_QTR_E = 3
		SET @CURRENT_QTR_DATE = CONVERT( DATETIME, '01/01/' + CONVERT(VARCHAR,@CURRENT_QTR_YEAR) )
		SET @CURRENT_QTR_END_DATE = CONVERT( DATETIME, '03/31/' + CONVERT(VARCHAR,@CURRENT_QTR_YEAR) )
	END
	ELSE IF ( @CURRENT_QTR_QTR = '2' )
	BEGIN
		SET @CURRENT_QTR_MONTH = 4
		SET @CURRENT_QTR_QTR_S = 2
		SET @CURRENT_QTR_QTR_E = 6
		SET @CURRENT_QTR_DATE = CONVERT( DATETIME, '04/01/' +  CONVERT(VARCHAR,@CURRENT_QTR_YEAR) )
		SET @CURRENT_QTR_END_DATE = CONVERT( DATETIME, '06/30/' + CONVERT(VARCHAR,@CURRENT_QTR_YEAR) )
	END
	ELSE IF ( @CURRENT_QTR_QTR = '3' )
	BEGIN
		SET @CURRENT_QTR_MONTH = 7
		SET @CURRENT_QTR_QTR_S = 3
		SET @CURRENT_QTR_QTR_E = 9
		SET @CURRENT_QTR_DATE = CONVERT( DATETIME, '07/01/' +  CONVERT(VARCHAR,@CURRENT_QTR_YEAR) )
		SET @CURRENT_QTR_END_DATE = CONVERT( DATETIME, '09/30/' + CONVERT(VARCHAR,@CURRENT_QTR_YEAR) )
	END
	ELSE IF ( @CURRENT_QTR_QTR = '4' )
	BEGIN
		SET @CURRENT_QTR_MONTH = 10
		SET @CURRENT_QTR_QTR_S = 4
		SET @CURRENT_QTR_QTR_E = 12
		SET @CURRENT_QTR_DATE = CONVERT( DATETIME, '10/01/' +  CONVERT(VARCHAR,@CURRENT_QTR_YEAR) )
		SET @CURRENT_QTR_END_DATE = CONVERT( DATETIME, '12/31/' + CONVERT(VARCHAR,@CURRENT_QTR_YEAR) )
	END

	PRINT ' @CURRENT_QTR_DATE ' + CONVERT(VARCHAR,@CURRENT_QTR_DATE,101) + ' @CURRENT_QTR_MONTH = ' + CONVERT(VARCHAR,@CURRENT_QTR_MONTH) + ' @CURRENT_QTR_QTR = ' + CONVERT(VARCHAR,@CURRENT_QTR_QTR) + ' @CURRENT_QTR_QTR_S = ' + CONVERT(VARCHAR,@CURRENT_QTR_QTR_S) + ' @CURRENT_QTR_QTR_E = ' + CONVERT(VARCHAR,@CURRENT_QTR_QTR_E) + ' @CURRENT_QTR_YEAR = ' + CONVERT(VARCHAR,@CURRENT_QTR_YEAR)
	
	IF @PRODUCT_IDS <> '0'
	BEGIN	
		INSERT INTO @SEARCH_RESULTS_TABLE 
		SELECT 
		( 
		SELECT PART_NUMBER FROM PRODUCT WHERE PRODUCT_ID = 
		CASE 
			WHEN P.P_PARENT_PRODUCT_ID = -1 THEN P.PRODUCT_ID 
			WHEN P.P_PARENT_PRODUCT_ID <> -1 THEN P.P_PARENT_PRODUCT_ID	
		END 
		) AS BASEPRODUCTNAME, 
		CASE 
			WHEN P.P_PARENT_PRODUCT_ID = -1 THEN P.PRODUCT_ID
			WHEN P.P_PARENT_PRODUCT_ID <> -1 THEN P.P_PARENT_PRODUCT_ID
		END ,
		P.PROCESSSTEP_ID, P.PRODUCT_ID, PART_NUMBER, PS_TYPE,
		CASE 
			WHEN P_TYPE IS NULL THEN 'CHIP'
			WHEN P_TYPE LIKE LTRIM(RTRIM('BOARD')) THEN 'BOARD'
			WHEN P_TYPE LIKE LTRIM(RTRIM('KIT')) THEN 'KIT'
		END AS PRODUCT_TYPE,
		CASE 
			WHEN P_TYPE IS NULL THEN 1
			WHEN P_TYPE LIKE LTRIM(RTRIM('BOARD')) THEN 2
			WHEN P_TYPE LIKE LTRIM(RTRIM('KIT')) THEN 3
		END AS PRODUCT_TYPE_ID, @CURRENT_QTR_DATE, 0, 0, 0, @CURRENT_QTR_MONTH, @CURRENT_QTR_YEAR, @CURRENT_QTR_QTR_S, @CURRENT_QTR_QTR_E, @CURRENT_QTR_YEAR, 1
		FROM PRODUCT P, PROCESS_STEP 
		WHERE P.PROCESSSTEP_ID = PROCESS_STEP.PROCESSSTEP_ID
		AND ( P_PARENT_PRODUCT_ID IN (SELECT ProcessStep_ID FROM @PROCESS_STEPS_TABLE) OR P.PRODUCT_ID = (SELECT ProcessStep_ID FROM @PROCESS_STEPS_TABLE) )
		AND P_PARENT_PRODUCT_ID IS NOT NULL 
		AND PS_TYPE_NAME = 'FINISHED GOODS'
		ORDER BY PRODUCT_TYPE_ID, BaseProductName 
	END
	ELSE
	BEGIN
		INSERT INTO @SEARCH_RESULTS_TABLE 
		SELECT 
		( 
		SELECT PART_NUMBER FROM PRODUCT WHERE PRODUCT_ID = 
		CASE 
			WHEN P.P_PARENT_PRODUCT_ID = -1 THEN P.PRODUCT_ID 
			WHEN P.P_PARENT_PRODUCT_ID <> -1 THEN P.P_PARENT_PRODUCT_ID	
		END
		) AS BASEPRODUCTNAME, 
		CASE 
			WHEN P.P_PARENT_PRODUCT_ID = -1 THEN P.PRODUCT_ID
			WHEN P.P_PARENT_PRODUCT_ID <> -1 THEN P.P_PARENT_PRODUCT_ID
		END ,
		P.PROCESSSTEP_ID, P.PRODUCT_ID, PART_NUMBER, PS_TYPE,
		CASE 
			WHEN P_TYPE IS NULL THEN 'CHIP'
			WHEN P_TYPE LIKE LTRIM(RTRIM('BOARD')) THEN 'BOARD'
			WHEN P_TYPE LIKE LTRIM(RTRIM('KIT')) THEN 'KIT'
		END AS PRODUCT_TYPE,
		CASE 
			WHEN P_TYPE IS NULL THEN 1
			WHEN P_TYPE LIKE LTRIM(RTRIM('BOARD')) THEN 2
			WHEN P_TYPE LIKE LTRIM(RTRIM('KIT')) THEN 3
		END AS PRODUCT_TYPE_ID, @CURRENT_QTR_DATE, 0, 0, 0, @CURRENT_QTR_MONTH, @CURRENT_QTR_YEAR, @CURRENT_QTR_QTR_S, @CURRENT_QTR_QTR_E, @CURRENT_QTR_YEAR, 1
		FROM PRODUCT P, PROCESS_STEP 
		WHERE P.PROCESSSTEP_ID = PROCESS_STEP.PROCESSSTEP_ID
		AND P_PARENT_PRODUCT_ID IS NOT NULL 
		AND PS_TYPE_NAME = 'FINISHED GOODS'
		ORDER BY PRODUCT_TYPE_ID, BaseProductName 
	END

	--select * from @SEARCH_RESULTS_TABLE
	
	SET @PARENT_PRODUCT_CURSOR = CURSOR FOR 
	SELECT BaseProductName, BaseProductID, ProcessStepID, ProductID, Product, PS_Type, Product_Type, Product_Type_ID FROM @SEARCH_RESULTS_TABLE
	ORDER BY Product_Type_ID, BaseProductName, ProcessStepID, ProductID


	OPEN @PARENT_PRODUCT_CURSOR 
	FETCH NEXT FROM @PARENT_PRODUCT_CURSOR INTO @RESULT_BASEPRODUCTNAME, @RESULT_BASE_PRODUCT_ID, @RESULT_PROCESS_STEP_ID, @RESULT_PRODUCT_ID, @RESULT_PRODUCT,
	@RESULT_PS_TYPE, @RESULT_PRODUCT_TYPE, @RESULT_PRODUCT_TYPE_ID
	WHILE @@FETCH_STATUS = 0
	BEGIN
		SET @REQUESTED_DATE = NULL
		SET @SCHEDULED_DATE = NULL
		SET @BOOKING_DATE   = NULL
		SET @TEMP_SHIPPED_QTY = NULL
		SET @TEMP_UNIT_COST = NULL
		SET @TEMP_LINEITEM_ID = NULL

		SET @SHIP_REQ_SCH_DT = NULL
		SET @SHIP_YR_I = NULL
		SET @SHIP_MT_I = NULL
		SET @SHIP_QT_I = NULL
		SET @SHIP_QT_END_I = NULL
		SET @ACT_YEAR = NULL

		--PRINT '  @RESULT_PRODUCT_ID = ' + CONVERT(VARCHAR,@RESULT_PRODUCT_ID) + '  @RESULT_PRODUCT = ' + @RESULT_PRODUCT
	
		IF ( @CUSTOMER_ID = '0' OR @CUSTOMER_ID = '' )
		BEGIN
			SET @LINE_ITEM_CURSOR = CURSOR FOR SELECT COOL.LineItem_ID, COOL.COOL_Unit_Cost, COOL.COOL_Qty, COOL.COOL_Requested_Date, ISNULL(COOL.COOL_Schedule_Date,'')
			FROM  Customer_Open_Order_LineItem COOL 
			INNER JOIN Sales_Order SO ON COOL.SalesOrder_ID = SO.SalesOrder_ID
			WHERE SO.SO_Is_Sample = 1 AND COOL.Product_ID = @RESULT_PRODUCT_ID AND COOL.COOL_Status <> 'SHIPPED'

		END
		ELSE
		BEGIN
			SET @LINE_ITEM_CURSOR = CURSOR FOR SELECT COOL.LineItem_ID, COOL.COOL_Unit_Cost, COOL.COOL_Qty, COOL.COOL_Requested_Date, ISNULL(COOL.COOL_Schedule_Date,'')
			FROM  Customer_Open_Order_LineItem COOL 
			INNER JOIN Sales_Order SO ON COOL.SalesOrder_ID = SO.SalesOrder_ID
			WHERE SO.Customer_ID = @CUSTOMER_ID AND SO.SO_Is_Sample = 1 AND COOL.Product_ID = @RESULT_PRODUCT_ID AND COOL.COOL_Status <> 'SHIPPED'
		END
		
		OPEN @LINE_ITEM_CURSOR 
		FETCH NEXT FROM @LINE_ITEM_CURSOR INTO @TEMP_LINEITEM_ID, @TEMP_UNIT_COST, @TEMP_SHIPPED_QTY, @REQUESTED_DATE, @SCHEDULED_DATE
		WHILE @@FETCH_STATUS = 0
		BEGIN
			SET @TEMP_DATE = NULL
			SET @PS_SHIPPED_QTY = NULL
			SET @IS_START_BACKLOG = 1
			IF @CATEGORY = 'SCHEDULE' 
			BEGIN	
				--PRINT '@SCHEDULED_DATE = ' + CONVERT( VARCHAR, @SCHEDULED_DATE ,101)
				IF( @SCHEDULED_DATE IS NULL OR @SCHEDULED_DATE = '' OR CONVERT(VARCHAR,@SCHEDULED_DATE,101) = '01/01/1900')
				BEGIN
					--SET @SHIP_REQ_SCH_DT = @CURRENT_QTR_DATE
					SET @SHIP_REQ_SCH_DT = CONVERT(DATETIME,CURRENT_TIMESTAMP)
				END
				ELSE
				BEGIN
					IF( @SCHEDULED_DATE < @CURRENT_QTR_DATE )
					BEGIN
						SET @SHIP_REQ_SCH_DT = @CURRENT_QTR_DATE
						SET @IS_START_BACKLOG = 0
					END
					ELSE
					BEGIN
						SET @SHIP_REQ_SCH_DT = @SCHEDULED_DATE
					END
				END
			END
			ELSE
			BEGIN
				IF( @REQUESTED_DATE < @CURRENT_QTR_DATE )
				BEGIN
					SET @SHIP_REQ_SCH_DT = @CURRENT_QTR_DATE
					SET @IS_START_BACKLOG = 0
				END
				ELSE
				BEGIN
					SET @SHIP_REQ_SCH_DT = @REQUESTED_DATE
				END
			END
			
			--SET @SHIP_REQ_SCH_DT = @BOOKING_DATE

			SET @SHIP_YR_I = DATEPART(YYYY, @SHIP_REQ_SCH_DT)
			SET @SHIP_MT_I = DATEPART(MM, @SHIP_REQ_SCH_DT)
			SET @ACT_YEAR = @SHIP_YR_I 
			
			IF (@SHIP_MT_I >=1 AND @SHIP_MT_I <=3)
			BEGIN
				SET @SHIP_QT_I = 1
				SET @SHIP_QT_END_I = 3
			END
			ELSE IF (@SHIP_MT_I >=4 AND @SHIP_MT_I <=6)
			BEGIN
				SET @SHIP_QT_I = 2
				SET @SHIP_QT_END_I = 6 
			END
			ELSE IF (@SHIP_MT_I >=7 AND @SHIP_MT_I <=9)
			BEGIN
				SET @SHIP_QT_I = 3
				SET @SHIP_QT_END_I = 9
			END						
			ELSE IF (@SHIP_MT_I >=10 AND @SHIP_MT_I <=12)
			BEGIN
				SET @SHIP_QT_I = 4
				SET @SHIP_QT_END_I = 12
			END
			
			IF( @SHIP_REQ_SCH_DT <= @CURRENT_QTR_END_DATE ) 			
			BEGIN

				SELECT @PS_SHIPPED_QTY = ISNULL( SUM( CSLIT_Qty_Shipped ), 0 ) from Customer_Shipping_Line_Item_Table where LineItem_ID = @TEMP_LINEITEM_ID and CSLIT_Status = 'SHIPPED'
	
				IF( @TEMP_SHIPPED_QTY - @PS_SHIPPED_QTY > 0 )
				BEGIN
					INSERT INTO @RESULTS_TABLE VALUES( @RESULT_BASEPRODUCTNAME, @RESULT_BASE_PRODUCT_ID, @RESULT_PROCESS_STEP_ID, @RESULT_PRODUCT_ID, @RESULT_PRODUCT,
					@RESULT_PS_TYPE, @RESULT_PRODUCT_TYPE, @RESULT_PRODUCT_TYPE_ID, @SHIP_REQ_SCH_DT, ( @TEMP_SHIPPED_QTY - @PS_SHIPPED_QTY ), @TEMP_UNIT_COST, 
					(  ( @TEMP_SHIPPED_QTY - @PS_SHIPPED_QTY ) * @TEMP_UNIT_COST ), @SHIP_MT_I, @SHIP_YR_I, @SHIP_QT_I, @SHIP_QT_END_I, @SHIP_YR_I, @IS_START_BACKLOG )
				END

			END

			SET @REQUESTED_DATE = NULL
			SET @SCHEDULED_DATE = NULL
			SET @BOOKING_DATE   = NULL

			SET @SHIP_REQ_SCH_DT = NULL
			SET @SHIP_YR_I = NULL
			SET @SHIP_MT_I = NULL
			SET @SHIP_QT_I = NULL
			SET @SHIP_QT_END_I = NULL
			SET @ACT_YEAR = NULL

			SET @TEMP_LINEITEM_ID = NULL
			SET @TEMP_UNIT_COST = NULL
			SET @TEMP_SHIPPED_QTY = NULL
			SET @BOOKING_DATE = NULL
			SET @REQUESTED_DATE = NULL
			SET @SCHEDULED_DATE = NULL
			SET @TEMP_DATE = NULL
			SET @PS_SHIPPED_QTY = NULL
			SET @IS_START_BACKLOG = 1
			
			FETCH NEXT FROM @LINE_ITEM_CURSOR INTO @TEMP_LINEITEM_ID, @TEMP_UNIT_COST, @TEMP_SHIPPED_QTY, @REQUESTED_DATE, @SCHEDULED_DATE
		END
		CLOSE @LINE_ITEM_CURSOR
		DEALLOCATE @LINE_ITEM_CURSOR
		
		SET @RESULT_BASEPRODUCTNAME = NULL
		SET @RESULT_BASE_PRODUCT_ID = NULL
		SET @RESULT_PROCESS_STEP_ID = NULL
		SET @RESULT_PRODUCT_ID = NULL
		SET @RESULT_PRODUCT = NULL
		SET @RESULT_PS_TYPE = NULL
		SET @RESULT_PRODUCT_TYPE = NULL
		SET @RESULT_PRODUCT_TYPE_ID = NULL

		FETCH NEXT FROM @PARENT_PRODUCT_CURSOR INTO @RESULT_BASEPRODUCTNAME, @RESULT_BASE_PRODUCT_ID, @RESULT_PROCESS_STEP_ID, @RESULT_PRODUCT_ID, @RESULT_PRODUCT,
		@RESULT_PS_TYPE, @RESULT_PRODUCT_TYPE, @RESULT_PRODUCT_TYPE_ID
	END
	CLOSE @PARENT_PRODUCT_CURSOR
	DEALLOCATE @PARENT_PRODUCT_CURSOR
	
	DECLARE @RESULTS_TABLE_COUNT INT	
	SELECT @RESULTS_TABLE_COUNT = COUNT(*) FROM @RESULTS_TABLE

	IF @RESULTS_TABLE_COUNT > 0
	BEGIN
		DELETE FROM @SEARCH_RESULTS_TABLE WHERE ProductID IN ( SELECT ProductID FROM @RESULTS_TABLE )

		--UPDATE @SEARCH_RESULTS_TABLE SET Req_Sch_Date = @SHIP_REQ_SCH_DT, Shipped_Qty = 0, Unit_Cost = 0, Amount = 0,
		--ShipmentMonth = @SHIP_MT_I,  ShipmentYear = @CURRENT_YEAR,  QuarterStart = @SHIP_QT_I,  QuarterEnd = @SHIP_QT_END_I,  ActYear = @CURRENT_YEAR
	
		INSERT INTO @SEARCH_RESULTS_TABLE SELECT * FROM @RESULTS_TABLE
	END
	/*ELSE
	BEGIN
		UPDATE @SEARCH_RESULTS_TABLE SET Req_Sch_Date = @SHIP_REQ_SCH_DT, Shipped_Qty = 0, Unit_Cost = 0, Amount = 0,
		ShipmentMonth = @SHIP_MT_I,  ShipmentYear = @CURRENT_YEAR,  QuarterStart = @SHIP_QT_I,  QuarterEnd = @SHIP_QT_END_I,  ActYear = @CURRENT_YEAR
	END*/

	/*IF @CURRENT_QUARTER = 'ALL'
	BEGIN
		SELECT * FROM @SEARCH_RESULTS_TABLE WHERE YEAR( Req_Sch_Date ) = @CURRENT_YEAR ORDER BY Product_Type_ID, BaseProductName, Product 
	END
	ELSE
	BEGIN
		SELECT * FROM @SEARCH_RESULTS_TABLE WHERE DATEPART(Q, Req_Sch_Date ) = @CURRENT_QUARTER  AND YEAR( Req_Sch_Date ) = @CURRENT_YEAR ORDER BY Product_Type_ID, BaseProductName, Product 
	END*/

	--SELECT * FROM @SEARCH_RESULTS_TABLE ORDER BY Product_Type_ID, BaseProductName, Product 
	SELECT *  FROM @SEARCH_RESULTS_TABLE WHERE DATEPART(Q, Req_Sch_Date ) = @CURRENT_QTR_QTR  AND YEAR( Req_Sch_Date ) = @CURRENT_QTR_YEAR ORDER BY Product_Type_ID, BaseProductName, Product 

END